<template>
  <FormDialogComponent
    ref="formDialogRef"
    dialogTitle="密码"
    v-model="form"
    :formHeader="formHeader"
    :formUrl="urls.API_URL_BACKSTAGE_USER_PASSWORD_EDIT"
    formAction="patch"
    :showFormFoot="false"
    :clearFormValueAfterSubmitSuccess="true"
  >
    <!--下面数据为测试保留-->
    <!-- <template #[`form_`+`password`]>
      测试2：
      <el-input v-model="form['password']" clearable />
    </template> -->
  </FormDialogComponent>
</template>

<script lang="ts" setup>
import FormDialogComponent from '@/components/base/table/form/FormDialogComponent.vue';
import { ref } from 'vue';
import type { FormHeader } from '@/types/interface/base_interface';
import type { UserPassword } from '@/types/interface/auth_interface';
import urls from '@/api/request/urls';

// 表单对象
const form = ref<UserPassword>({
  password: '',
  newPassword: '',
  confirmPassword: ''
});
// 密码验证规则
const validateConfirmPass = (rule: any, value: any, callback: any) => {
  if (value !== form.value?.newPassword) {
    callback(new Error('确认密码必须与原密码一致'));
  } else {
    callback();
  }
};

// 表单元素
const formHeader = ref<FormHeader[]>([
  {
    label: '原密码',
    prop: 'password',
    type: 'password',
    rules: [{ required: true, message: '不能为空', trigger: 'blur' }]
  },
  {
    label: '新密码',
    prop: 'newPassword',
    type: 'password',
    rules: [
      { required: true, message: '不能为空', trigger: 'blur' },
      {
        min: 6,
        max: 20,
        message: '密码长度必须在6-20位之间',
        trigger: 'blur'
      }
    ]
  },
  {
    label: '确认密码',
    prop: 'confirmPassword',
    type: 'password',
    rules: [
      { required: true, message: '不能为空', trigger: 'blur' },
      { validator: validateConfirmPass, trigger: 'blur' }
    ]
  }
]);
// 表单ref对象
const formDialogRef = ref(); // 表单ref对象
// 打开对话框，gong供父组件调用，传入表单对象
const openDialog = () => {
  formDialogRef.value!.openDialog();
};
// 暴露组件给父容器使用
defineExpose({
  openDialog
});
</script>

<style lang="scss" scoped></style>
